package com.atguigu.flink.datastreamapi.transform;

import com.atguigu.flink.function.WaterSensorMapFunction;
import com.atguigu.flink.pojo.WaterSensor;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;

/**
 * Created by Smexy on 2022/12/13
 *  process:
 *          最底层算子。 强大，灵活，需要编程的多！
 *
 *   -----------------------------
 *      希望统计所有传感器(不分类型)的vc和
 */
public class Demo9_Process1
{
    public static void main(String[] args) {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

                env
                   .socketTextStream("hadoop103", 8888)
                   .map(new WaterSensorMapFunction())
                   //.global()
                   .process(new ProcessFunction<WaterSensor, String>()
                   {
                       //属性
                       int sum = 0;
                       /*
                            WaterSensor value: 当前的数据
                            Context ctx：  上下文(编程环境)，从这里获取一些额外的信息
                            Collector<String> out： 收集输出，发送到下游

                        */
                       @Override
                       public void processElement(WaterSensor value, Context ctx, Collector<String> out) throws Exception {

                           sum += value.getVc();

                           out.collect("总和:"+sum);

                       }
                   }).setParallelism(1)
                   .print();

                try {
                            env.execute();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }


    }
}
